import { defineStore } from 'pinia'

// 定义一个 Pinia 状态管理器，名为 'alive'
const useAliveStore = defineStore('alive', {
	state: () => {
		return {
			// 缓存的组件名称列表
			keepAliveRoutes: [] as string[],
			// 当前路由的唯一标识符
			routeKey: null as string | null,
			// 控制路由显示状态
			routeShow: true as boolean
		}
	},
	actions: {
		// 添加组件到缓存列表中，避免重复添加
		pushKeepLive(component: string) {
			if (!this.keepAliveRoutes.includes(component)) {
				this.keepAliveRoutes.push(component)
			}
		},
		// 从缓存列表中移除组件
		removeKeepLive(component: string) {
			this.keepAliveRoutes = this.keepAliveRoutes.filter((item) => item !== component)
		},
		// 清空缓存的所有组件
		clearKeepLive() {
			this.keepAliveRoutes = []
		},
		// 设置当前路由的 key，用于标识路由的唯一性
		setRouteKey(key: string | null) {
			this.routeKey = key
		},
		// 控制路由的显示状态
		setRouteShow(key: boolean) {
			this.routeShow = key
		}
	}
})

// 导出定义的 store 以供使用
export default useAliveStore
